<?php
/*
    Copyright (C) 2008 - 2010 Inanta Martsanto
    
    This file is part of Koma.

    Koma is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Koma is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Koma.  If not, see <http://www.gnu.org/licenses/>.
    
    Inanta Martsanto (inanta@inationsoft.com)
*/

abstract class KomaComponent extends KomaObject
{
    function initialize($args = array())
    {
        $this->createProperties(array('Database' => KomaDatabaseFactory::getInstance(),
                                      'Template' => KomaTemplate::getInstance(),
                                      'Session' => KomaSession::getInstance(),
                                      'Page' => KomaPage::getInstance(),
                                      'Render' => true,
                                      'Directory' => $args['dir'],
                                      'Version' => $args['version'],
                                      'URL' => KOMA_URL.'/components/'.$args['dir'].'/'));
        
        $this->setReadOnlyProperties(array('Database', 'Template', 'Session'));
        gettext_init(KOMA_LOCALE, $args['dir'].'.main', KOMA_ROOT_PATH.'/components/'.$args['dir'].'/locales', KOMA_CHARSET);
    }
    
    function finalize()
    {
        if($this->Template->File != null) $this->Template->display(KOMA_ROOT_PATH.'/components/'.$this->Directory.'/templates/'.$this->Template->File);
    }
    
    function getParam($param, $method = 'p')
    {
        switch ($method)
        {
            case 'post':
            case 'p':
                if(!empty($_POST[$param])) return trim($_POST[$param]);
                break;
            case 'get':
            case 'g':
                if(isset($_GET[$param])) return trim($_GET[$param]);
                break;
            case 'request':
            case 'r':
            default:
                if(isset($_REQUEST[$param]) && !empty($_REQUEST[$param])) return trim($_REQUEST[$param]);
        }
        
        return false;
    }
    
    function getParams($params = null, $method = 'p')
    {
        $ret = array();
        
        if(!is_array($params))
        {
            switch($method)
            {
                case 'post':
                case 'p':
                    $params = $_POST;
                    break;
                case 'get':
                case 'g':
                    $params = $_GET;
                    break;
                case 'request':
                case 'r':
                default:
                    $params = $_REQUEST;
            }
            
            foreach($params as $key => $value) $ret[$key] = $value;
            return $ret;
        }
        
        foreach($params as $param) if(!$ret[$param] = $this->getParam($param, $method)) return false;
        return $ret;
    }
    
    function main()
    {
        throw new KomaOverrideMethodException(__FUNCTION__);
    }
}
?>